using System;
using System.Collections.Generic;
using System.Text;
using ESBSimpleSamples.ServiceClient.Enums;
using ESBSimpleSamples.ServiceClient.Helpers;

namespace ESBSimpleSamples.ServiceClient.Itineraries
{
    public class ResolverConnection
    {
        #region Private instance fields

        private Dictionary<string, string> _connectionSettings;
        private ResolverType _type = ResolverType.Null;

        #endregion
        
        #region Private instance properties

        private Dictionary<string, string> ConnectionSettings
        {
            get
            {
                if (this._connectionSettings == null)
                {
                    this._connectionSettings = new Dictionary<string, string>(8);
                    this._connectionSettings.Add(ConnectionSetting.TransportType, null);
                    this._connectionSettings.Add(ConnectionSetting.TransportLocation, null);
                    this._connectionSettings.Add(ConnectionSetting.Action, null);
                    this._connectionSettings.Add(ConnectionSetting.EndPointConfig, null);
                    this._connectionSettings.Add(ConnectionSetting.JaxRpcResponse, null);
                    this._connectionSettings.Add(ConnectionSetting.MessageExchangePattern, null);
                    this._connectionSettings.Add(ConnectionSetting.TargetNamespace, null);
                    this._connectionSettings.Add(ConnectionSetting.TransformType, null);
                    this._connectionSettings.Add(ConnectionSetting.ServerUrl, null);
                    this._connectionSettings.Add(ConnectionSetting.ServiceProvider, null);
                    this._connectionSettings.Add(ConnectionSetting.ServiceName, null);
                }
                return this._connectionSettings;
            }
        }

        #endregion 

        #region Public instance properties

        public ResolverType ResolverType
        {
            get { return this._type; }
            set { this._type = value; }
        }

        public string TransportType
        {
            get { return this.ConnectionSettings[ConnectionSetting.TransportType]; }
            set { this.ConnectionSettings[ConnectionSetting.TransportType] = value; }
        }

        public string TransportLocation
        {
            get { return this.ConnectionSettings[ConnectionSetting.TransportLocation]; }
            set { this.ConnectionSettings[ConnectionSetting.TransportLocation] = value; }
        }
  
        public string Action
        {
            get { return this.ConnectionSettings[ConnectionSetting.Action]; }
            set { this.ConnectionSettings[ConnectionSetting.Action] = value; }
        }

        public string EndPointConfig
        {
            get { return this.ConnectionSettings[ConnectionSetting.EndPointConfig]; }
            set { this.ConnectionSettings[ConnectionSetting.EndPointConfig] = value; }
        }
        
        public string JaxRpcResponse
        {
            get { return this.ConnectionSettings[ConnectionSetting.JaxRpcResponse]; }
            set { this.ConnectionSettings[ConnectionSetting.JaxRpcResponse] = value; }
        }
        
        public string MessageExchangePattern
        {
            get { return this.ConnectionSettings[ConnectionSetting.MessageExchangePattern]; }
            set { this.ConnectionSettings[ConnectionSetting.MessageExchangePattern] = value; }
        }

        public string TargetNamespace
        {
            get { return this.ConnectionSettings[ConnectionSetting.TargetNamespace]; }
            set { this.ConnectionSettings[ConnectionSetting.TargetNamespace] = value; }
        }

        public string TransformType
        {
            get { return this.ConnectionSettings[ConnectionSetting.TransformType]; }
            set { this.ConnectionSettings[ConnectionSetting.TransformType] = value; }
        }

        public string ServerUrl
        {
            get { return this.ConnectionSettings[ConnectionSetting.ServerUrl]; }
            set { this.ConnectionSettings[ConnectionSetting.ServerUrl] = value; }
        }

        public string ServiceProvider
        {
            get { return this.ConnectionSettings[ConnectionSetting.ServiceProvider]; }
            set { this.ConnectionSettings[ConnectionSetting.ServiceProvider] = value; }
        }

        public string ServiceName
        {
            get { return this.ConnectionSettings[ConnectionSetting.ServiceName]; }
            set { this.ConnectionSettings[ConnectionSetting.ServiceName] = value; }
        }

        #endregion

        #region Constructors

        /// <summary>
        /// Default constructor
        /// </summary>
        public ResolverConnection() {}

        /// <summary>
        /// Constructor with known type
        /// </summary>
        /// <param name="type"></param>
        public ResolverConnection(ResolverType type)
        {
            this._type = type;
        }

        #endregion

        #region Public instance methods

        public string GetConnectionString()
        {
            StringBuilder connectionStringBuilder = new StringBuilder();
            //the connection string starts with the moniker, then any specified settings, 
            //semi-colon delimited; note, connection string may be blank if no resolver specified:
            if (this._type != ResolverType.Null)
            {
                connectionStringBuilder.AppendFormat(@"{0}:\\", EnumHelper.GetCode(this._type));
                foreach (KeyValuePair<string, string> connectionSetting in this.ConnectionSettings)
                {
                    if (!string.IsNullOrEmpty(connectionSetting.Value))
                    {
                        connectionStringBuilder.AppendFormat("{0}={1};", connectionSetting.Key, connectionSetting.Value);
                    }
                }
            }
            return connectionStringBuilder.ToString();
        }
        #endregion

        #region Public structs

        public struct ConnectionSetting
        {
            public const string TransportType = "TransportType";
            public const string TransportLocation = "TransportLocation";
            public const string Action = "Action";
            public const string EndPointConfig = "EndPointConfig";
            public const string JaxRpcResponse = "JaxRpcResponse";
            public const string MessageExchangePattern = "MessageExchangePattern";
            public const string TargetNamespace = "TargetNamespace";
            public const string TransformType = "TransformType";
            public const string ServerUrl = "ServerUrl";
            public const string ServiceProvider = "ServiceProvider";
            public const string ServiceName = "ServiceName";
        }

        #endregion
    }
}
